home *** CD-ROM | disk | FTP | other *** search
- //
- // MiscString_ExtendedParsing.m -- substrings with token elimiters
- // Written by Thomas Engel Copyright (c) 1994 by Thomas Engel.
- // Version 1.00. All rights reserved.
- // This notice may not be removed from this source code.
- //
- // This object is included in the MiscKit by permission from the author
- // and its use is governed by the MiscKit license, found in the file
- // "LICENSE.rtf" in the MiscKit distribution. Please refer to that file
- // for a list of all applicable permissions and restrictions.
- //
-
- #import <misckit/MiscString.h>
-
- @implementation MiscString(ExtendedParsing)
-
- - substringFromToken:(int)start toToken:(int)end ofList:aList
- {
- // We want to get the substring between two tokens of the tokenList.
- // If there is no tokenlist we will do a standard tokenization.
- // If you pass a wrong list and wrong numbers you might get wrong
- // results!
-
- BOOL mustFreeList = NO;
- int i;
- id substring;
- id aString;
- id token;
-
- if( !aList )
- {
- mustFreeList = YES;
- aList = [self tokenize:" \t\n\r" into:nil];
- }
- // Now check the token numbers. we could return nil in these cases but
- // We'll try to find something 'useful'.
-
- if( start > end )
- {
- i = start;
- start = end;
- end = i;
- }
- if( end > [aList count]-1 ) end = [aList count]-1;
- if( end < 0 ) end = 0;
- if( start > [aList count]-1 ) start = [aList count]-1;
- if( start < 0 ) start = 0;
-
- // Ok. from what pos to what pos does our substring range.
- // Lets reduce it step by step from the edges.
- // If the token List has no items at all we'll do nothing
-
- substring = [self copy];
- if( [aList count] != 0 )
- {
- for( i=0; i<start+1; i++ )
- {
- // We will try to remove every token up to the wanted.
- // But we will have to remove leading spaces etc. too.
-
- token = [aList objectAt:i];
- do
- {
- if( [substring length] < 1 ) break;
-
- if( [substring compareTo:token
- n:[token length] caseSensitive:YES] == 0 ) break;
-
- // Seems like the substing does not start with the wanted token
- // So lets remove one char and try it again.
-
- aString = [substring midFrom:1 to:[substring length]-1];
- [substring free];
- substring = aString;
- }
- while( YES );
-
- // If this is not the token we are searching for...remove it.
-
- if( i != start )
- {
- aString = [substring midFrom:[token length]
- to:[substring length]-1];
- [substring free];
- substring = aString;
- }
- }
-
- // Now lets kill the unimportant stuff to the right...
- // No big deal same as before.
-
- for( i=[aList count]-1; i>end-1; --i )
- {
- // We will try to remove every token up to the wanted.
- // But we will have to remove leading spaces etc. too.
-
- token = [aList objectAt:i];
- do
- {
- if( [substring length] < 1 ) break;
-
- if( [substring endCompareTo:token n:[token length]
- caseSensitive:YES] == 0 ) break;
-
- // Seems like the substing does not start with the wanted token
- // So lets remove one char and try it again.
-
- aString = [substring midFrom:0 to:[substring length]-2];
- [substring free];
- substring = aString;
- }
- while( YES );
-
- // If this is not the token we are searching for...remove it.
-
- if( i != end )
- {
- aString = [substring midFrom:0
- to:[substring length] - [token length] - 1];
- [substring free];
- substring = aString;
- }
- }
- }
-
- // Now lets free the list if needed.
-
- if( mustFreeList ) [aList free];
-
- return substring;
- }
-
- @end
-